package com.pcbly.web.components.utils.template;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.pcbly.web.bean.crm.BxdBean;
import com.pcbly.web.components.utils.ClassUtilsStatic;

@SuppressWarnings("unchecked")
public class ModuleGenerators {
	final static String packName = "com.pcbly.web.components.utils.template";
	final static String[] fileName = { "action.txt", "xml_struts_config.txt",
			"applicationContext_dao.txt", "applicationContext_service.txt",
			"applicationContext_struts.txt", "dao_impl.txt", "dao.txt",
			"ServiceImp.txt", "Service.txt" };
	private Class clz;

	private String entity;

	private String entityP;

	private String entityL;

	private String firstEntityL;

	private String clzName;

	private String tableName;

	public Class getClz() {
		return clz;
	}

	public void setClz(Class clz) {
		this.clz = clz;
	}

	public String getEntity() {
		return entity;
	}

	public void setEntity(String entity) {
		this.entity = entity;
	}

	public String getEntityP() {
		return entityP;
	}

	public void setEntityP(String entityP) {
		this.entityP = entityP;
	}

	public String getEntityL() {
		return entityL;
	}

	public void setEntityL(String entityL) {
		this.entityL = entityL;
	}

	public String getFirstEntityL() {
		return firstEntityL;
	}

	public void setFirstEntityL(String firstEntityL) {
		this.firstEntityL = firstEntityL;
	}

	public String getClzName() {
		return clzName;
	}

	public void setClzName(String clzName) {
		this.clzName = clzName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public static String getPackname() {
		return packName;
	}

	public static String[] getFilename() {
		return fileName;
	}

	protected String actionXmlPath() {
		String packageName = "com.pcbly.web.action." + this.entityL;
		String name = this.firstEntityL + "-applicationContext-struts.xml";
		return getFilePath(packageName, name);
	}

	protected String actionStrutsPath() {
		String packageName = "com.pcbly.web.action." + this.entityL;
		String name = this.firstEntityL + "-struts.xml";
		return getFilePath(packageName, name);
	}

	protected String actionPath() {
		String packageName = "com.pcbly.web.action." + this.entityL;
		String name = this.entityP + "Action.java";
		return getFilePath(packageName, name);
	}

	protected String daoXmlPath() {
		String packageName = "com.pcbly.web.persistence." + this.entityL;
		String name = this.firstEntityL + "-applicationContext-dao.xml";
		return getFilePath(packageName, name);
	}

	protected String daoImplPath() {
		String packageName = "com.pcbly.web.persistence." + this.entityL;
		String name = this.entityP + "HibernateDao.java";
		return getFilePath(packageName, name);
	}

	protected String daoPath() {
		String packageName = "com.pcbly.web.persistence." + this.entityL;
		String name = this.entityP + "Dao.java";
		return getFilePath(packageName, name);
	}

	protected String servicePath() {
		String packageName = "com.pcbly.web.business." + this.entityL;
		String name = this.entityP + "Service.java";
		return getFilePath(packageName, name);
	}

	protected String serviceXmlPath() {
		String packageName = "com.pcbly.web.business." + this.entityL;
		String name = this.firstEntityL + "-applicationContext-service.xml";
		return getFilePath(packageName, name);
	}

	protected String serviceImpPath() {
		String packageName = "com.pcbly.web.business." + this.entityL;
		String name = this.entityP + "ServiceImp.java";
		return getFilePath(packageName, name);
	}

	protected String getFilePath(String packageName, String name) {
//		System.out.println("replace:" + packageName);
		String path = packageName.replaceAll("\\.", "/");
//		System.out.println("after relpace:" + path);
		return "src/" + path + "/" + name;
	}

	String readTpl(File tpl) {
		return readTpl(tpl, "UTF-8");
	}

	String readTpl(File tpl, String charset) {
		String content = null;
		try {
			content = FileUtils.readFileToString(tpl, charset);
			content = content.replaceAll("\\#\\{entityL\\}", this.entityL);
			content = content.replaceAll("\\#\\{entityP\\}", this.entityP);
			content = content.replaceAll("\\#\\{clzName\\}", this.clzName);
			content = content.replaceAll("\\#\\{entity\\}", this.entity);
			content = content.replaceAll("\\#\\{tableName\\}", this.tableName);
			content = content.replaceAll("\\#\\{firstEntityL\\}",
					this.firstEntityL);

		} catch (IOException e) {
			System.out.println("read file faild. " + e.getMessage());
		}
		return content;
	}
	
	public void execute(Class clz) {
		try { 
			String pachageString = clz.getPackage().getName();
			String path = pachageString.replaceAll("\\.", "/");
			String pacha = clz.getPackage().getName();
			File file = new File("src/" + path);
			
			if (file.isDirectory()) {
				String[] files = file.list();
				for (String fi : files) {
					if (fi.indexOf("java") >= 0) {
//						System.out.println("::::::::::::::::::::::::::::::::::::::");
						String te = fi.substring(0, fi.indexOf("."));
						te = pacha + "." + te;
						Class temp = ClassUtilsStatic.forName(te);
						this.clz = temp;
//						te = temp.getName();
//						te = te.substring(te.lastIndexOf(".")+1, te.length());
						this.clzName =  temp.getName();
//						System.out.println(this.clzName);
						this.entity = this.clzName.substring(this.clzName.lastIndexOf(".") + 1,
								this.clzName.length());
//						System.out.println("entity >>>>>>>>>>> "+this.entity);
						String[] args = this.clzName.split("\\.");
						this.entityL = args[args.length - 2];
//						System.out.println("entityL >>>>>>>>>>> "+this.entityL);
						this.entityP = this.entity.substring(0,this.entity.length()-4);
//						System.out.println("entityP >>>>>>>>>>> "+this.entityP);
						this.firstEntityL=this.entityP.substring(0,1).toLowerCase()+this.entityP.substring(1,this.entityP.length());
//						System.out.println("firstEntityL >>>>>>>>>>> "+this.firstEntityL);
						this.tableName="t_"+this.entityL+"_"+this.entityP.toLowerCase();
//						System.out.println("tableName >>>>>>>>>>> "+this.tableName);
						execute();
//						break;
					}
				}
				for(String string:actionXMLString){
					System.out.println(string);
				}
				System.out.println("::::::::::::::::::::::::::::::::::::::");
				for(String string:actionStrutsString){
					System.out.println(string);
				}
				System.out.println("::::::::::::::::::::::::::::::::::::::");
				for(String string:daoXmlString){
					System.out.println(string);
				}
				System.out.println("::::::::::::::::::::::::::::::::::::::");
				for(String string:serviceXmlString){
					System.out.println(string);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private List<String> actionXMLString=new ArrayList<String>();
	
	private List<String> actionStrutsString=new ArrayList<String>();
	
	private List<String> daoXmlString=new ArrayList<String>();
	
	private List<String> serviceXmlString=new ArrayList<String>();
	
	public List<String> getActionXMLString() {
		return actionXMLString;
	}

	public void setActionXMLString(List<String> actionXMLString) {
		this.actionXMLString = actionXMLString;
	}

	public List<String> getActionStrutsString() {
		return actionStrutsString;
	}

	public void setActionStrutsString(List<String> actionStrutsString) {
		this.actionStrutsString = actionStrutsString;
	}

	public List<String> getDaoXmlString() {
		return daoXmlString;
	}

	public void setDaoXmlString(List<String> daoXmlString) {
		this.daoXmlString = daoXmlString;
	}

	public List<String> getServiceXmlString() {
		return serviceXmlString;
	}

	public void setServiceXmlString(List<String> serviceXmlString) {
		this.serviceXmlString = serviceXmlString;
	}

	protected void execute() {
		try {
			File file = new File(actionXmlPath());
			// System.out.println(file.getAbsolutePath());
			/************** applicationContext_struts.txt **************************/ 
			String name = "applicationContext_struts.txt";
			StringBuilder sb = new StringBuilder();
			String pathXml=getFilePath(packName, name);
			actionXMLString.add("<import resource=\""+actionXmlPath()+"\"/>");
			File fileXML = new File(pathXml);
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** struts.txt **************************/
			file = new File(actionStrutsPath());
			name = "struts.txt";
			pathXml=getFilePath(packName, name);
			actionStrutsString.add("<include file=\""+actionStrutsPath()+"\"/>");
			fileXML = new File(pathXml);
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** action.txt **************************/
			file = new File(actionPath());
			name = "action.txt";
			fileXML = new File(getFilePath(packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** applicationContext_dao.txt **************************/
			file = new File(daoXmlPath());
			name = "applicationContext_dao.txt";
			pathXml=getFilePath(packName, name);
			daoXmlString.add("<import resource=\""+daoXmlPath()+"\"/>");
			fileXML = new File(pathXml);
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** dao_impl.txt **************************/
			file = new File(daoImplPath());
			name = "dao_impl.txt";
			fileXML = new File(getFilePath(packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** dao.txt **************************/
			file = new File(daoPath());
			name = "dao.txt";
			fileXML = new File(getFilePath(packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** Service.txt **************************/
			file = new File(servicePath());
			name = "Service.txt";
			fileXML = new File(getFilePath(packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** Service.txt **************************/
			file = new File(serviceXmlPath());
			name = "applicationContext_service.txt";
			pathXml=getFilePath(packName, name);
			serviceXmlString.add("<import resource=\""+serviceXmlPath()+"\"/>");
			fileXML = new File(pathXml);
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			/************** Service.txt **************************/
			file = new File(serviceImpPath());
			name = "ServiceImp.txt";
			fileXML = new File(getFilePath(packName, name));
			sb.append(readTpl(fileXML));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());

			if (file != null)
				file = null;

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {

		ModuleGenerators module = new ModuleGenerators();
		module.execute(BxdBean.class);
	}
}
